package com.wanchuang.hepos.ui.updateutil;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.wanchuang.hepos.R;

import kale.ui.view.dialog.BaseCustomDialog;
import kale.ui.view.dialog.BaseEasyDialog;
import kale.ui.view.dialog.EasyDialog;

public class UpDateDialog extends BaseCustomDialog {
    public OnUpdateClickListener dialogListener;
    public static final String KEY_CONTENT = "KEY_CONTENT";
    public static final String KEY_FORCE = "KEY_FORCE";

    /**
     * 继承自{@link EasyDialog.Builder}以扩展builder
     */
    public static class Builder extends BaseEasyDialog.Builder<Builder> {

        private Bundle bundle = new Bundle();
        private OnUpdateClickListener listener;
        public Builder(@NonNull Context context) {
            super(context);
        }


        public Builder setListener(OnUpdateClickListener lis) {
            listener = lis;
            return this;
        }
        public Builder setContent(String content) {
            bundle.putString(KEY_CONTENT, content);
            return this;
        }

        public Builder setForce(boolean force) {
            bundle.putBoolean(KEY_FORCE, force);
            return this;
        }

        @NonNull
        @Override
        protected EasyDialog createDialog() {
            UpDateDialog dialog = new UpDateDialog();
            dialog.setArguments(bundle); // 增加自己的bundle
            dialog.dialogListener = listener;
            return dialog;
        }
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_update;
    }

    @Override
    protected void bindViews(View view) {
        ImageView closeView = findView(R.id.iv_close);

        closeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        if (getArguments().getBoolean(KEY_FORCE,false)) closeView.setVisibility(View.GONE);
        Button btnCommit = findView(R.id.btn_commit);
        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogListener.onClick(v);
            }
        });
        TextView tv_msg = findView(R.id.tv_msg);
        tv_msg.setText(getArguments().getString(KEY_CONTENT));
    }

    @Override
    protected void setViews() {
        setBackground();
        setLayout();

    }

    /**
     * 强制取消背景，保持有透明
     */
    private void setBackground() {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable()); // 去除dialog的背景，即透明
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0xffffffff)); // 设置白色背景
//            getDialog().getWindow().setBackgroundDrawableResource(R.drawable.dialog_shop_jion_bg); // 设置背景
    }


    /**
     * 也可通过setLayout来设置：
     * getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
     */
    private void setLayout() {
        Window window = getDialog().getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER; // 设置展示的位置
        window.setAttributes(lp);
    }

    @Override
    protected void modifyAlertDialogBuilder(AlertDialog.Builder builder) {
        super.modifyAlertDialogBuilder(builder);
    }

    //  监听输入框内容
    public interface OnUpdateClickListener {
        void onClick(View v);
    }

}
